package tenant

import (
	"context"
	pb "git.mycaigou.com/gfyx/micro-gfyx-user-service/proto/micro_gfyx_user_service_proto/micro-gfyx-user-service/tenant"
	"git.mycaigou.com/gfyx/micro-gfyx-user-service/service/tenant/create_tenant"
	"git.myscrm.cn/golang/stark/v4"
)

type createTenantServer struct {
	tenantsService create_tenant.CreateTenantServiceIface
}

func NewCreateTenantServer(
	tenantsService create_tenant.CreateTenantServiceIface,
) pb.CreateTenantServiceServer {
	return &createTenantServer{
		tenantsService: tenantsService,
	}
}

func (s *createTenantServer) CreateTenant(ctx context.Context, request *pb.CreateTenantRequest) (*pb.CreateTenantResponse, error) {
	resp, err := s.tenantsService.CreateTenant(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc CreateTenant request:%+v, err:%s", request, err.Error())
		return &pb.CreateTenantResponse{}, err
	}
	return resp, nil
}

// RemoveTenant 销户
func (s *createTenantServer) RemoveTenant(ctx context.Context, request *pb.RemoveTenantRequest) (*pb.RemoveTenantResponse, error) {
	resp, err := s.tenantsService.RemoveTenant(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc RemoveTenant request:%+v, err:%s", request, err.Error())
		return &pb.RemoveTenantResponse{}, err
	}
	return resp, nil
}

// CreateTenantCallback 开库结果回调
func (s *createTenantServer) CreateTenantCallback(ctx context.Context, request *pb.CreateTenantCallbackRequest) (*pb.CreateTenantCallbackResponse, error) {
	resp, err := s.tenantsService.CreateTenantCallback(ctx, request, 0)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc CreateTenantCallback request:%+v, err:%s", request, err.Error())
		return &pb.CreateTenantCallbackResponse{}, err
	}
	return resp, nil
}

// GetTaskStatus 获取任务状态
func (s *createTenantServer) GetTaskStatus(ctx context.Context, request *pb.GetTaskStatusRequest) (*pb.GetTaskStatusResponse, error) {
	resp, err := s.tenantsService.GetTaskStatus(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc GetTaskStatus request:%+v, err:%s", request, err.Error())
		return &pb.GetTaskStatusResponse{}, err
	}
	return resp, nil
}
